package com.ali.service.impl;

import com.ali.api.PayApi;
import com.ali.common.Response;
import com.ali.common.ReturnEnum;
import com.ali.dao.OrderMapper;
import com.ali.dto.OrderInfo;
import com.ali.dto.PayInfo;
import com.ali.entity.Order;
import com.ali.service.OrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private PayApi payApi;

    @Override
    public Response<OrderInfo> query(Map<String, Object> param) {
        String id = (String) param.get("id");
        Order order = orderMapper.selectById(id);
        OrderInfo orderInfo = new OrderInfo();
        BeanUtils.copyProperties(order, orderInfo);

        Map<String, Object> payParam = new HashMap<>() {{
            put("orderid", param.get("id"));
        }};

        //内部服务调用
        Response<List<PayInfo>> payResponse = payApi.query(payParam);
        orderInfo.setPayInfos(payResponse.getData());
        return Response.success(orderInfo);
    }

    @Override
    public Response<Map<String, Object>> timeout() {
        Map<String, Object> map = new HashMap<>();

        map.put("开始时间", LocalDateTime.now());
        try {
            //内部服务调用
            payApi.timeout();
        } catch (Exception e) {
            log.error(e.getLocalizedMessage(), e);
            map.put("结束时间", LocalDateTime.now());
            return Response.fail(ReturnEnum.RC999.getCode(), "连接超时", map);
        }

        return Response.success(map);
    }

    @Override
    public Response<String> control(Map<String, Object> param) {
        return payApi.control(param);
    }

    @Override
    public Response<String> print(Map<String, Object> param, Map<String, Object> header) {
        return payApi.print(param, header);
    }
}
